package org.cuatrovientos.erp.dao;

import java.util.List;

import org.cuatrovientos.erp.model.Stock;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author Enrico Baroni
 * <Stock>'s DAO.
 */
public class StockDAO implements GenericInterfaceDAO {

	@Autowired
	 private SessionFactory sessionFactory;
	
	/**
	 * Get list's <Stock>
	 */
	@Override
	public List<Stock> selectAll() {

	    List<Stock> stocks = getSession().createQuery("from stock").list();
	    return stocks;
	}

	/**
	 * Get an <Stock>
	 */
	@Override
	public Stock selectById(int id) {
		Stock stock = (Stock) getSession().get(Stock.class, id);
	    return stock;
	}

	/**
	 * delete an <Stock>
	 */
	@Override
	public void delete(int id) {
		Stock stock = selectById(id);
		getSession().delete(stock);
		
	}

	/**
	 * Insert an <Stock>
	 */
	@Override
	public void insert(Object newStock) {
		 Integer id = (Integer) getSession().save(newStock);
		    ((Stock) newStock).setId(id);  
		
	}

	/**
	 * update an <Stock>
	 */
	@Override
	public void update(Object stock) {
		getSession().merge(stock);
		
	}

	/**
	 * Get a session.
	 */
	@Override
	public Session getSession() {
		 return sessionFactory.getCurrentSession();
	}

	

}
